﻿using Newtonsoft.Json;
using System.Drawing;
using System.Text;
using System.Web;
using ZXing;
using ZXing.Common;
using ZXing.ZKWeb;
namespace Wechat.Webapi
{
    public static   class OtherUtils
    {
        public static void Debug(string info, string tag = "debug")
        {
            System.Diagnostics.Debug.WriteLine(tag + " " + info);
        }

        /// <summary>
        /// debug输出错误，并且返回格式化的错误
        /// </summary>
        /// <param name="err"></param>
        /// <returns></returns>
        public static string Debug(Exception err)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("****************************异常文本****************************");
            sb.AppendLine("【出现时间】：" + DateTime.Now.ToString());
            if (err != null)
            {
                sb.AppendLine("【异常类型】：" + err.GetType().Name);
                sb.AppendLine("【异常信息】：" + err.Message);
                sb.AppendLine("【堆栈调用】：" + err.StackTrace);
            }
            sb.AppendLine("***************************************************************");
            System.Diagnostics.Debug.WriteLine(sb.ToString());
            return sb.ToString();
        }

        /// <summary>
        /// 获取时间戳，10位数字
        /// </summary>
        /// <returns></returns>
        public static string GetTimeStamp()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalSeconds).ToString();
        }

        /// <summary>
        /// 模拟获取微信接口中的r
        /// </summary>
        /// <returns></returns>
        public static int Get_r()
        {
            string timespan = OtherUtils.GetTimeStamp();
            return (int)(Convert.ToInt64(timespan) / 1579);
        }

        /// <summary>  
        /// 获取时间戳，13位数字，兼容java
        /// </summary>  
        /// <returns></returns>  
        public static Int64 GetJavaTimeStamp()
        {
            return Convert.ToInt64(GetTimeStamp() + GetRandomNumber(3));
        }

        /// <summary>
        /// 时间戳转datetime
        /// </summary>
        /// <param name="timeStamp"></param>
        /// <returns></returns>
        public static DateTime StampToDateTime(string timeStamp)
        {
            DateTime dateTimeStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(timeStamp + "0000000");
            TimeSpan toNow = new TimeSpan(lTime);
            return dateTimeStart.Add(toNow);
        }

        /// <summary>
        /// 生成随机数，保证第一位不是0
        /// </summary>
        /// <param name="num">长度</param>
        /// <returns></returns>
        public static string GetRandomNumber(int length)
        {
            char[] Pattern = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
            //, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
            string randcode = "";
            int n = Pattern.Length;
            Random random = new Random(Guid.NewGuid().GetHashCode());
            for (int i = 0; i < length; i++)
            {
                int rnd;
                if (i != 0)
                    rnd = random.Next(0, n);
                else
                    rnd = random.Next(1, n);
                randcode += Pattern[rnd];
            }
            return randcode;
        }

        public static string GetFileMD5Hash(string fileName)
        {
            return GetFileMD5Hash(new FileInfo(fileName));
        }
        public static string GetFileMD5Hash(FileInfo fileInfo)
        {
            try
            {
                FileStream file = fileInfo.OpenRead();
                System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
                byte[] retVal = md5.ComputeHash(file);
                file.Close();

                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < retVal.Length; i++)
                {
                    sb.Append(retVal[i].ToString("x2"));
                }
                return sb.ToString();
            }
            catch (Exception ex)
            {
                throw new Exception("GetMD5Hash() fail,error:" + ex.Message);
            }
        }


        public static string UrlEncode(string urlToEncode)
        {
            if (string.IsNullOrEmpty(urlToEncode))
                return urlToEncode;
            return HttpUtility.UrlEncode(urlToEncode, Encoding.UTF8);
        }

        public static string UrlDecode(string urlToDecode)
        {
            if (string.IsNullOrEmpty(urlToDecode))
                return urlToDecode;
            return HttpUtility.UrlDecode(urlToDecode, Encoding.UTF8);
        }

        public static long GetTimestamp()
        {
            // 获取当前时间
            DateTime currentTime = DateTime.UtcNow;

            // Unix时间戳起始时间
            DateTime unixStartTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);

            // 计算当前时间与Unix起始时间的时间差
            TimeSpan timeSpan = currentTime - unixStartTime;

            // 将时间差的总秒数作为Unix时间戳
            long timestamp = (long)timeSpan.TotalSeconds;

            return timestamp;
        }

        /// <summary>
        /// 生成二维码 base64
        /// </summary>
        /// <param name="message">二维码信息</param>
        /// <param name="size">尺寸</param>
        /// <returns></returns>

        public static string QRCode(this string content, int size = 300)
        {

            if (string.IsNullOrWhiteSpace(content)) return string.Empty;
            BarcodeWriter writer = new()
            {
                Format = BarcodeFormat.QR_CODE,
                Options = new EncodingOptions() { Margin = 0, Width = size, Height = size, PureBarcode = false }
            };
            string qrCodeImageAsBase64 = Convert.ToBase64String(writer.ToByte(content));
            return $"data:image/png;base64,{qrCodeImageAsBase64}";
        }
        private static byte[] ToByte(this BarcodeWriter code, string message)
        {
            using (System.DrawingCore.Bitmap bmp = code.Write(message))
            {
                byte[]? byteArray = null;
                using (MemoryStream stream = new())
                {

                    bmp.Save(stream, System.DrawingCore.Imaging.ImageFormat.Png);
                    byteArray = stream.GetBuffer();
                }
                return byteArray;

            }

        }
        /// <summary>
        /// 对象转为字符串
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToNullString(this object obj)
        {
            if (obj != null || obj == DBNull.Value)
            {
                string val = obj.ToString();
                if (val == null) return string.Empty;
                return val.Trim();
            }
            return string.Empty;
        }

        /// <summary>
        /// 对象转换整数
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int ToInt(this object obj)
        {
            if (obj == null || obj == DBNull.Value) return 0;
            if (typeof(decimal) == obj.GetType())
                obj = Math.Floor((decimal)obj);
            else if (typeof(double) == obj.GetType())
                obj = Math.Floor((double)obj);
            else if (typeof(string) == obj.GetType() && obj.ToNullString().IndexOf('.') > -1)
                obj = obj.ToNullString().Substring(0, obj.ToNullString().IndexOf('.'));
            string nullString = obj.ToNullString();
            if (int.TryParse(nullString, out int result))
            {
                return result;
            }
            return 0;
        }
        public static string ToJson(this object dta)
        {
            return JsonConvert.SerializeObject(dta);
        }
        public static T ToObject<T>(this string dta)
        {
            return JsonConvert.DeserializeObject<T>(dta);
        }
    }
}
